import BannerAd from "./BannerAd";

const { ccclass, property } = cc._decorator;

@ccclass
export default class TreasureBox extends cc.Component {
    @property(cc.Sprite)
    jdt: cc.Sprite = null;//进度条
    gived: boolean = false;
    isAd = false;
    fun: Function = null;

    public static Show(fun: Function) {
        cc.resources.load("Prefabs/TreasureBox", cc.Prefab, (err, date) => {
            if (err) {
                console.log("没有找到宝箱预制体");
                return;
            }
            console.log("生成宝箱");
            let pre = cc.instantiate(date);
            var treasureBox = pre.getComponent(TreasureBox);
            treasureBox.fun = fun;
            treasureBox.isAd = false;
            treasureBox.gived = false;
            treasureBox.Show();
            pre.setParent(cc.find("Canvas"));
            pre.setSiblingIndex(99999);
            pre.setPosition(0, 0, 0);
        })

    }

    Show() {
        cc.tween(this.node.getChildByName("其他").getChildByName("光效"))
            .to(2, { angle: 360 })
            .to(0, { angle: 0 })
            .union()
            .repeatForever()
            .start();
    }

    //点击宝箱按钮
    OnButtonClick() {
        this.jdt.fillRange += 0.1;
        if (this.jdt.fillRange >= 0.2 && !this.isAd) {
            this.isAd = true;
            BannerAd.Instance.ShowCustomAd();
        }
        if (this.jdt.fillRange >= 0.6 && !this.gived) {
            this.gived = true;
            BannerAd.Instance.ShowVideo(() => { });
            this.scheduleOnce(() => {
                this.fun && this.fun();
                this.node.destroy();
            }, 1);
        }
    }

    protected update(dt: number): void {
        this.jdt.fillRange -= 0.005;
        if (this.jdt.fillRange < 0) this.jdt.fillRange = 0;
    }
}
